<?php
/**
 * communit.as
 * @copyright (C)2008 Jaybill McCarthy, All Rights Reserved.
 * @category communitas
 * @package communitas
 * @author Jaybill McCarthy
 * @link http://communit.as communit.as
 * @license http://communit.as/docs/license License
 */

/**
 *
 * @package communitas
 * @subpackage forum_controllers
 * @license http://communit.as/docs/license License 
 */

class Forum_IndexController extends Forum_Controller_Action_Abstract {

	function init(){
		parent::init();
		
	}

	function rerouteAction(){
		$this->_redirect('/forum');
	}

	function searchAction(){
		$this->view->pagetitle = "Search Results";
		// all topics in this forum
		$request = new Cts_Request($this->getRequest());
		if($this->getRequest()->isPost()){
			$this->_redirect('/forum/index/search/searchterm/'.$request->searchterm);
		}
		$this->view->searchterm = $request->searchterm;
		$forums_table = new Forums();
		$posts_table = new Posts();
		$views_index_table = new TopicviewsIndex();
		$views_table = new Topicviews();
		$where = $forums_table->getAdapter()->quoteInto('id = ?',$request->id);
		$topics_table = new TopicsIndex();
		$topics_where = 'title like "%'.$request->searchterm.'%"';
		//dd($topics_where);
		$per_page = Cts_Registry::get('forum_topics_per_page','forum');
		if($request->has('page')){
			$page = $request->page;
		} else {
			$page = 0;
		}
		$total = $topics_table->getCountByWhereClause($topics_where);		
		$url = "/forum/index/search";
		$params = array('searchterm' => $request->searchterm);
		$this->makePager($page,$per_page,$total,$url,$params);

		$topics = $topics_table->fetchAll($topics_where,null,$per_page,$per_page * $page);
		if(count($topics) > 0){

			$tmp_topics = array();
			foreach($topics as $topic){
				$tmp_topic = $topic->toArray();
				$tmp_topic['replies'] = ($posts_table->getCountByWhereClause('topic_id = '.$topic->id)) - 1;
				$tmp_topic['views'] = (int)$views_index_table->getCount($topic->id);

				$last_post = $posts_table->getLatestByTopicId($topic->id, null);

				if(!is_null($last_post)){
					$tmp_topic['last_post'] = $last_post->toArray();
					$last_viewed = $views_table->getLastTimeViewed($tmp_topic['id']);
					$last_post_created =  strtotime($tmp_topic['last_post']['created_on']);
					if($last_viewed == null or $last_post_created > $last_viewed){
						$tmp_topic['new'] = true;
					}
				}
				$tmp_topics[] = $tmp_topic;
			}
			$this->view->topics = $tmp_topics;
		}
	}

	function rssAction(){
		$posts_table = new Posts();
		$topics_table = new Topics();
		$forums_table = new Forums();
		$posts = $posts_table->fetchAll(null,'created_on desc',20);
		$tmp_posts = array();
		if(count($posts) > 0){
			foreach($posts as $post){
				$tmp_post = $post->toArray();
				$topic = $topics_table->fetchRow('id = '.$post->topic_id);
				$forum_where = $forums_table->getAdapter()->quoteInto('id = ?',$topic->forum_id);
				$forum = $forums_table->fetchRow($forum_where);
				if($post->is_topic == 1){
					$tmp_post['title'] = "New Topic in '".$forum->title."' - '".$topic->title."'";
				} else {
					$tmp_post['title'] = "Response to '".$topic->title."' in '".$forum->title."'";
				}
				$tmp_post['author_display_name'] = $topic->username;
				$tmp_post['pubdate'] = date("r",strtotime($post->created_on));
				$tmp_post['url'] = Cts_Registry::get('site_url') . "/forum/topic/index/id/".$topic->id;
				$user_link = Cts_Registry::get('site_url') ."/profile/".$post->username;
				$user_link = "<a href=\"$user_link\">".$post->username."</a>";
				$tmp_post['post'] = $user_link ." wrote: ".$this->_filter->filter($tmp_post['post']);
				$tmp_posts[] = $tmp_post;			}
			
			$this->view->posts = $tmp_posts;
		}	
		header('Content-Type: application/rss+xml');
		
	}
	
	function indexAction(){

		// all forums
		$forums_table = new Forums();
		$subjects_table = new Subjects();
		$forum_totals_table = new ForumTotals();
		$subjects = $subjects_table->fetchAll();
		$topics_index_table = new TopicsIndex();
		$views_table = new Topicviews();
		$posts_table = new Posts();

		if(count($subjects) > 0){
			$tmp_subjects = array();
			foreach($subjects as $subject){
				$tmp_forums = array();
				$forums = $forums_table->fetchAll("subject_id = '".$subject->id."' and active = 1","order");
				if(count($forums) > 0){
					foreach($forums as $forum){
						$tmp_forum = $forum->toArray();
						$totals = $forum_totals_table->fetchRow('forum_id = "'.$forum->id.'"');
						$tmp_forum['topic_count'] = $totals->topic_count;
						$tmp_forum['post_count'] = $totals->post_count;

						$latest_topic = $topics_index_table->fetchRow('forum_id = "'.$forum->id.'"','last_post_created_on desc');
						if(!is_null($latest_topic)){
							$last_post = $posts_table->getLatestByTopicId($latest_topic->id, null);
							if(!is_null($last_post)){
								$tmp_forum['last_post'] = $last_post->toArray();
								$last_viewed = $views_table->getLastTimeViewed($latest_topic->id);
								$last_post_created =  strtotime($tmp_forum['last_post']['created_on']);
								if($last_viewed == null or $last_post_created > $last_viewed){
									$tmp_forum['new'] = true;
								}
							}
						}
						$tmp_forums[] = $tmp_forum;
					}
				}
				$tmp_subjects[$subject->id]['forums'] = $tmp_forums;
			}
			$this->view->subjects = $tmp_subjects;
		}
	}

}
